package com.common.www.controller.project;

import org.springframework.boot.base.jpa.service.IBaseService;
import com.common.www.bean.entity.Result;
import com.common.www.bean.entity.project.XfgcBiddingCompany;
import com.common.www.bean.entity.tabulation.XfgcBidBaseTabulation;
import com.common.www.bean.entity.tabulation.XfgcTabulation;
import com.common.www.bean.vo.xfgc.StatisticalXfgcBiddingVo;
import com.common.www.controller.base.DefaultCrudController;
import com.common.www.data.NetCode;
import com.common.www.service.project.XfgcBiddingCompanyService;
import com.common.www.service.tabulation.*;
import com.common.www.utils.ParamsUtils;
import com.common.www.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@CrossOrigin
@RestController
@RequestMapping(value = {"mgt/xfgcBiddingCompany"})
@Api(tags = "MGT 消防工程绑定供应商管理接口")
public class MGTXfgcBiddingCompanyController extends DefaultCrudController<XfgcBiddingCompany, Long> {
    @Autowired
    private XfgcBiddingCompanyService service;
    @Autowired
    private XfgcBidBaseTabulationService xfgcBidBaseTabulationService;
    @Autowired
    private XfgcTabulationService xfgcTabulationService;


    protected IBaseService getService() {
        return service;
    }


    @ApiOperation("批量添加项目供应商")
    @RequestMapping(value = "/saveByJsonBatch", method = RequestMethod.POST)
    public Result saveByJsonBatch(@RequestBody List<XfgcBiddingCompany> biddingCompanys) {
        for (XfgcBiddingCompany biddingCompany : biddingCompanys) {
            if (biddingCompany.getId() != null) {
                // 更新操作
                XfgcBiddingCompany existing = service.getByIdLogic(biddingCompany.getId());
                if (existing == null) {
                    throw new ServiceException("未找到目标对象");
                }
                // 复制属性到现有对象，排除ID字段
                BeanUtils.copyProperties(biddingCompany, existing, "id");
                // 保存现有对象
                service.save(existing);
            } else {
                // 新增操作，直接保存传入的对象
                service.save(biddingCompany);
            }
        }
        return R.ok(biddingCompanys);
    }

    @ApiOperation("编辑项目供应商状态")
    @RequestMapping(value = "/saveByJson", method = RequestMethod.POST)
    public Result saveByJson(@RequestBody XfgcBiddingCompany biddingCompany) {
        if (biddingCompany.getId() != null) {
            // 更新操作
            XfgcBiddingCompany existing = service.getByIdLogic(biddingCompany.getId());
            if (existing == null) {
                throw new ServiceException("未找到目标对象");
            }
            // 复制属性到现有对象，排除ID字段
            BeanUtils.copyProperties(biddingCompany, existing, "id");
            // 保存现有对象
            service.save(existing);
            return R.ok(existing);
        } else {
            // 新增操作，直接保存传入的对象
            service.save(biddingCompany);
            return R.ok(biddingCompany);
        }
    }


    @ApiOperation("批量删除启用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "String", defaultValue = ""),
    })
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    public Result batchDelete(Long[] ids) {
        if (ParamsUtils.isInputEmpty(ids)) {
            return R.returnCode(NetCode.CODE_INPUT_OMIT);
        }
        for (int i = 0; i < ids.length; i++) {
            XfgcBiddingCompany biddingCompany = service.findOne(ids[i]);
            if (biddingCompany != null) {
                biddingCompany.setFlag(0);
                service.save(biddingCompany);
                //删除子信息
                List<XfgcTabulation> xfgcTabulations = xfgcTabulationService.getRepository().findByFlagAndXfgcBiddingCompanyId(1,ids[i]);
//                List<TabulationExtracting> tabulationExtractings = tabulationExtractingService.getRepository()
//                        .findByFlagAndProjectIdAndSupplyCompanyIdAndInnerOuterType(1,biddingCompany.getProject().getId(),biddingCompany.getSupplyCompany().getId(),biddingCompany.getInnerOuterType());
                if (xfgcTabulations != null && xfgcTabulations.size() > 0) {
                    xfgcTabulationService.delete(xfgcTabulations);
                }
//                if (tabulationExtractings != null && tabulationExtractings.size() > 0) {
//                    tabulationExtractingService.delete(tabulationExtractings);
//                }
            }
        }
        return R.ok();
    }



    @ApiOperation(value = "查询清单信息(供应商等组合信息)", nickname = "", notes = "")
    @ApiImplicitParams(value = {
    })
    @RequestMapping(value = "/query2", produces = {"application/json"}, method = RequestMethod.POST)
    public Result query2(Long projectId) {
        HashMap hashMap = new HashMap();
        hashMap.put("project.id", projectId);
        List<XfgcBiddingCompany> companyList = service.getListLogic(hashMap, null);
        //查询标底
        List<XfgcBidBaseTabulation> xfgcBidBaseTabulations1 = xfgcBidBaseTabulationService.getRepository().findByFlagAndProjectId(1, projectId);
        Map<String, Object> mapXfgcBidBase1 = xfgcBidBaseTabulationService.getInformation(xfgcBidBaseTabulations1, 1);

        XfgcBiddingCompany item = null;
        if (companyList.size() == 0) {
            if (xfgcBidBaseTabulations1.size() > 0){
                item = new XfgcBiddingCompany();
                item.setXfgcBidBaseTabulation1(xfgcBidBaseTabulations1.size() > 0 ? mapXfgcBidBase1 : null);
                companyList.add(item);
            }
        } else {
            for (XfgcBiddingCompany company : companyList) {
                company.setXfgcBidBaseTabulation1(xfgcBidBaseTabulations1.size() > 0 ? mapXfgcBidBase1 : null);

                //查询内外回标清单(回标1、回标2、...)
                List<XfgcTabulation> xfgcTabulations1 = xfgcTabulationService.getRepository().findByFlagAndXfgcBiddingCompanyIdAndJieDuan(1, company.getId(), 2);
                List<XfgcTabulation> xfgcTabulations2 = xfgcTabulationService.getRepository().findByFlagAndXfgcBiddingCompanyIdAndJieDuan(1, company.getId(), 3);
                List<XfgcTabulation> xfgcTabulations3 = xfgcTabulationService.getRepository().findByFlagAndXfgcBiddingCompanyIdAndJieDuan(1, company.getId(), 4);
                List<XfgcTabulation> xfgcTabulations4 = xfgcTabulationService.getRepository().findByFlagAndXfgcBiddingCompanyIdAndJieDuan(1, company.getId(), 5);

                StatisticalXfgcBiddingVo mapXfgcT1 = xfgcTabulationService.getInformation(xfgcTabulations1, 2);
                StatisticalXfgcBiddingVo mapXfgcT2 = xfgcTabulationService.getInformation(xfgcTabulations2, 3);
                StatisticalXfgcBiddingVo mapXfgcT3 = xfgcTabulationService.getInformation(xfgcTabulations3, 4);
                StatisticalXfgcBiddingVo mapXfgcT4 = xfgcTabulationService.getInformation(xfgcTabulations4, 5);

                company.setXfgcTabulation1(xfgcTabulations1.size() > 0 ? mapXfgcT1 : null);
                company.setXfgcTabulation2(xfgcTabulations2.size() > 0 ? mapXfgcT2 : null);
                company.setXfgcTabulation3(xfgcTabulations3.size() > 0 ? mapXfgcT3 : null);
                company.setXfgcTabulation4(xfgcTabulations4.size() > 0 ? mapXfgcT4 : null);
            }
        }
        List<XfgcBiddingCompany> sortedList = companyList.stream()
                .filter(vo -> vo.getBidStatus() != null)
                .sorted(Comparator.comparingInt(XfgcBiddingCompany::getBidStatus))
                .collect(Collectors.toList());
        return R.ok(sortedList);
    }


}

